//
//  JGJHistoryMatchDetailTableViewCell.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/13.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit

class JGJHistoryMatchDetailTableViewCell: UITableViewCell {
    @IBOutlet weak var leftCount: UILabel!
    @IBOutlet weak var rightCount: UILabel!
    @IBOutlet weak var TypeString: UILabel!
    @IBOutlet weak var leftValueViewWidth: NSLayoutConstraint!  //通过改变他的multiplier 来改变长度
    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    func setCell(teamADataModel:MatchDataModel,teamBDataModel:MatchDataModel,valueType:String)  {
        TypeString.text = valueType
        switch valueType {
               case "射门":
                leftCount.text = teamADataModel.matchDataShoot ?? "0"
                rightCount.text = teamBDataModel.matchDataShoot ?? "0"
                   break;
               case "射正":
                leftCount.text = teamADataModel.matchDataShootIn ?? "0"
                rightCount.text = teamBDataModel.matchDataShootIn ?? "0"
                   break;
               case "越位":
                leftCount.text = teamADataModel.matchDataOffside ?? "0"
                rightCount.text = teamBDataModel.matchDataOffside ?? "0"
                   break;
               case "角球":
                leftCount.text = teamADataModel.matchDataCornnerKick ?? "0"
                rightCount.text = teamBDataModel.matchDataCornnerKick ?? "0"
                   break;
               case "犯规":
                leftCount.text = teamADataModel.matchDataFoul ?? "0"
                rightCount.text = teamBDataModel.matchDataFoul ?? "0"
                   break;
               case "黄牌":
                   leftCount.text = teamADataModel.matchDataYellowCard ?? "0"
                   rightCount.text = teamBDataModel.matchDataYellowCard ?? "0"
                   break;
               case "红牌":
                   leftCount.text = teamADataModel.matchDataRedCard ?? "0"
                   rightCount.text = teamBDataModel.matchDataRedCard ?? "0"
                   break;   

               default:
                   return
               }
        
//计算数值    multiplier = 左边进球数/（左边进球数+右边进球数）
        var num:CGFloat = 0.5
        let Acount = Int(leftCount.text ?? "0")!
        let Bcount = Int(rightCount.text ?? "0")!
        if (Acount + Bcount ) != 0 {
            num = CGFloat(Acount)/CGFloat((Acount + Bcount))
        }

        leftValueViewWidth = leftValueViewWidth.setMultiplier(multiplier: num)
    }


}


extension NSLayoutConstraint {
    /**
     Change multiplier constraint

     - parameter multiplier: CGFloat
     - returns: NSLayoutConstraint
    */
    func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint {

        NSLayoutConstraint.deactivate([self])

        let newConstraint = NSLayoutConstraint(
            item: firstItem,
            attribute: firstAttribute,
            relatedBy: relation,
            toItem: secondItem,
            attribute: secondAttribute,
            multiplier: multiplier,
            constant: constant)

        newConstraint.priority = priority
        newConstraint.shouldBeArchived = self.shouldBeArchived
        newConstraint.identifier = self.identifier

        NSLayoutConstraint.activate([newConstraint])
        return newConstraint
    }
}
